package com.atguigu.gmall.cart.controller.api;

import com.atguigu.gmall.cart.service.CartApiService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/cart")
public class CartApiController {

    @Autowired
    private CartApiService cartApiService;

    /**
     * /api/cart/addToCart/{skuId}/{skuNum}
     * 加入购物车
     * @param skuId
     * @param skuNum
     * @param request
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    @ApiOperation("加入购物车")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request)
    {
        //获取用户账户信息的id
        String userId = AuthContextHolder.getUserId(request);
        //判断userId有没有,如果没有说明用户未登陆,获取临时id
        if(StringUtils.isEmpty(userId)){
            //获取临时id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartApiService.addToCart(skuId,skuNum,userId);
        return Result.ok();
    }

    /**
     * api/cart/cartList
     * 查询购物车列表
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    @ApiOperation("查询购物车列表")
    public Result cartList(HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        //获取临时id
        String userTempId = AuthContextHolder.getUserTempId(request);
        //调用service进行购物车列表查询
        List<CartInfo> cartInfoList=cartApiService.getCartList(userId,userTempId);
        //把购物车列表进行返回
        return Result.ok(cartInfoList);

    }

    //编写勾选状态
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    @ApiOperation("勾选状态")
    public Result checkCart(
            @PathVariable Long skuId,
            @PathVariable Integer isChecked,
            HttpServletRequest request
    ){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId=AuthContextHolder.getUserTempId(request);
        }
        cartApiService.checkCart(userId,skuId,isChecked);
        return Result.ok();
    }

    //删除购物车项
    @DeleteMapping("/deleteCart/{skuId}")
    @ApiOperation("删除购物车项")
    public Result deleteCart(@PathVariable Long skuId,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if(userId==null){
            userId=AuthContextHolder.getUserTempId(request);
        }
        cartApiService.deleteCart(userId,skuId);
        return Result.ok();
    }


    /**
     * 根据用户Id 查询购物车列表
     * /api/cart/getCartCheckedList/{userId}
     * @param userId
     * @return
     */
    @GetMapping("/getCartCheckedList/{userId}")
    @ApiOperation("根据用户Id 查询购物车列表")
    public List<CartInfo> getCartCheckedList(@PathVariable Long userId){
        return cartApiService.getCartCheckedList(userId);
    }

}
